package model;

import java.awt.Point;
import java.util.List;
import java.util.Observable;

/**
 * The Classic Map is the default world map being played in Risk. It contains
 * all the continents and territories.
 * 
 * @author David Shefchik, Xiaotian Chang
 * 
 */
public class ClassicMap extends Map {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7737974485678836383L;
	private Continent northAmerica;
	private Continent southAmerica;
	private Continent africa;
	private Continent europe;
	private Continent asia;
	private Continent oceania;

	/**
	 * Calls the constructor of the Map superclass and calls buildMap()
	 * 
	 * @param players
	 *            is a ArrayList which contains all players in this game.
	 */
	public ClassicMap(List<Player> players) {
		super(players);
		buildMap();
		super.setPlayerList();
		int numPlayer = players.size();
		if (numPlayer == 2) {
			for (Player tempPlayer : players) {
				tempPlayer.addBonus(30);
			}
		}
		if (numPlayer == 3) {
			for (Player tempPlayer : players) {
				tempPlayer.addBonus(35);
			}
		}
		if (numPlayer == 4) {
			for (Player tempPlayer : players) {
				tempPlayer.addBonus(30);
			}
		}
		if (numPlayer == 5) {
			for (Player tempPlayer : players) {
				tempPlayer.addBonus(25);
			}
		}
		if (numPlayer == 6) {
			for (Player tempPlayer : players) {
				tempPlayer.addBonus(20);
			}
		}
		addWildCard(2);
		super.assignCardType();
	}

	/**
	 * The implementation of the BuildMap method of Map. This method is
	 * hard-coded specifically for each interchangeable map. This is building
	 * the Default Classic Map. First, it creates all continents, then creates
	 * all territories with the appropriate continent references and creates a
	 * list of all their neighbors. It then adds all the continents to the map.
	 * It returns nothing.
	 */
	public void buildMap() {
		// create six continents.
		northAmerica = new Continent("North America", this);
		northAmerica.setBonusTroops(5);

		southAmerica = new Continent("South America", this);
		southAmerica.setBonusTroops(2);

		africa = new Continent("Africa", this);
		africa.setBonusTroops(3);

		europe = new Continent("Europe", this);
		europe.setBonusTroops(5);

		asia = new Continent("Asia", this);
		asia.setBonusTroops(7);

		oceania = new Continent("Oceania", this);
		oceania.setBonusTroops(2);

		buildTerritories();
		// create 9 territories in north America.
		this.addContinent(northAmerica);

		// create 4 territories in south America.
		this.addContinent(southAmerica);

		// create 7 territories in south America.
		this.addContinent(europe);

		// create 6 territories in Africa.
		this.addContinent(africa);

		// create 12 territories in Asia.
		this.addContinent(asia);

		// create 4 territories in Oceania.
		this.addContinent(oceania);
	}

	private void buildTerritories() {

		// Oceania
		Territory easternAustralia;
		Territory indonesia;
		Territory newGuinea;
		Territory westernAustralia;

		easternAustralia = new Territory("Eastern Australia", oceania, this);

		indonesia = new Territory("Indonesia", oceania, this);
		newGuinea = new Territory("New Guinea", oceania, this);
		westernAustralia = new Territory("Western Australia", oceania, this);
		easternAustralia.setColorSignature(-12582848);
		easternAustralia.setPoint(new Point(658, 313));
		indonesia.setColorSignature(-8388353);
		indonesia.setPoint(new Point(572, 253));
		newGuinea.setColorSignature(-65281);
		newGuinea.setPoint(new Point(676, 242));
		westernAustralia.setColorSignature(-8388544);
		westernAustralia.setPoint(new Point(592, 313));
		// Asia
		Territory afghanistan;
		Territory china;
		Territory india;
		Territory irkutsk;
		Territory japan;
		Territory kamchatka;
		Territory middleEast;
		Territory mongolia;
		Territory siam;
		Territory siberia;
		Territory ural;
		Territory yakutsk;

		afghanistan = new Territory("Afghanistan", asia, this);
		china = new Territory("China", asia, this);
		india = new Territory("India", asia, this);
		irkutsk = new Territory("Irkutsk", asia, this);
		japan = new Territory("Japan", asia, this);
		kamchatka = new Territory("Kamchatka", asia, this);
		middleEast = new Territory("Middle East", asia, this);
		mongolia = new Territory("Mongolia", asia, this);
		siam = new Territory("Siam", asia, this);
		siberia = new Territory("Siberia", asia, this);
		ural = new Territory("Ural", asia, this);
		yakutsk = new Territory("Yakutsk", asia, this);
		afghanistan.setColorSignature(-16718751);
		afghanistan.setPoint(new Point(440, 103));
		china.setColorSignature(-16470456);
		china.setPoint(new Point(558, 143));
		india.setColorSignature(-16760832);
		india.setPoint(new Point(485, 160));
		irkutsk.setColorSignature(-8323328);
		irkutsk.setPoint(new Point(529, 75));
		japan.setColorSignature(-5308672);
		japan.setPoint(new Point(644, 134));
		kamchatka.setColorSignature(-16744384);
		kamchatka.setPoint(new Point(619, 47));
		middleEast.setColorSignature(-16744448);
		middleEast.setPoint(new Point(388, 157));
		mongolia.setColorSignature(-15549696);
		mongolia.setPoint(new Point(542, 102));
		siam.setColorSignature(-8323200);
		siam.setPoint(new Point(571, 197));
		siberia.setColorSignature(-16761551);
		siberia.setPoint(new Point(475, 44));
		ural.setColorSignature(-16662593);
		ural.setPoint(new Point(437, 58));
		yakutsk.setColorSignature(-16753881);
		yakutsk.setPoint(new Point(542, 49));

		// Africa
		Territory centralAfrica;
		Territory eastAfrica;
		Territory egypt;
		Territory madagascar;
		Territory northAfrica;
		Territory southAfrica;

		centralAfrica = new Territory("Central Africa", africa, this);
		eastAfrica = new Territory("East Africa", africa, this);
		egypt = new Territory("Egypt", africa, this);
		madagascar = new Territory("Madagascar", africa, this);
		northAfrica = new Territory("North Africa", africa, this);
		southAfrica = new Territory("South Africa", africa, this);
		centralAfrica.setColorSignature(-5351680);
		centralAfrica.setPoint(new Point(337, 237));
		eastAfrica.setColorSignature(-32768);
		eastAfrica.setPoint(new Point(360, 198));
		egypt.setColorSignature(-8372224);
		egypt.setPoint(new Point(336, 159));
		madagascar.setColorSignature(-1620437);
		madagascar.setPoint(new Point(413, 302));
		northAfrica.setColorSignature(-28325);
		northAfrica.setPoint(new Point(277, 180));
		southAfrica.setColorSignature(-11589373);
		southAfrica.setPoint(new Point(339, 299));

		// Europe
		Territory britain;
		Territory iceland;
		Territory northernEurope;
		Territory scandinavia;
		Territory southernEurope;
		Territory ukraine;
		Territory westernEurope;

		britain = new Territory("Great Britain", europe, this);
		iceland = new Territory("Iceland", europe, this);
		northernEurope = new Territory("Northern Europe", europe, this);
		scandinavia = new Territory("Scandinavia", europe, this);
		southernEurope = new Territory("Southern Europe", europe, this);
		ukraine = new Territory("Ukraine", europe, this);
		westernEurope = new Territory("Western Europe", europe, this);
		britain.setColorSignature(-16760704);
		britain.setPoint(new Point(245, 70));
		iceland.setColorSignature(-12178451);
		iceland.setPoint(new Point(276, 29));
		northernEurope.setColorSignature(-16776961);
		northernEurope.setPoint(new Point(321, 85));
		scandinavia.setColorSignature(-16744193);
		scandinavia.setPoint(new Point(307, 42));
		southernEurope.setColorSignature(-16627630);
		southernEurope.setPoint(new Point(332, 124));
		ukraine.setColorSignature(-16777088);
		ukraine.setPoint(new Point(372, 66));
		westernEurope.setColorSignature(-9785099);
		westernEurope.setPoint(new Point(248, 111));

		// South America
		Territory argentina;
		Territory brazil;
		Territory peru;
		Territory venezuela;
		argentina = new Territory("Argentina", southAmerica, this);
		brazil = new Territory("Brazil", southAmerica, this);
		peru = new Territory("Peru", southAmerica, this);
		venezuela = new Territory("Venezuela", southAmerica, this);
		argentina.setColorSignature(-65536);
		argentina.setPoint(new Point(185, 319));
		brazil.setColorSignature(-8372160);
		brazil.setPoint(new Point(214, 256));
		peru.setColorSignature(-8388608);
		peru.setPoint(new Point(175, 271));
		venezuela.setColorSignature(-32640);
		venezuela.setPoint(new Point(161, 201));

		// North America
		Territory alaska;
		Territory alberta;
		Territory centralAmerica;
		Territory easternUnitedStates;
		Territory greenland;
		Territory northwestTerritory;
		Territory ontario;
		Territory quebec;
		Territory westernUnitedStates;
		alaska = new Territory("Alaska", northAmerica, this);
		alberta = new Territory("Alberta", northAmerica, this);
		centralAmerica = new Territory("Central America", northAmerica, this);
		greenland = new Territory("Greenland", northAmerica, this);
		ontario = new Territory("Ontario", northAmerica, this);
		quebec = new Territory("Quebec", northAmerica, this);
		westernUnitedStates = new Territory("Western United States",
				northAmerica, this);
		easternUnitedStates = new Territory("Eastern United States",
				northAmerica, this);
		northwestTerritory = new Territory("Northwest Territory", northAmerica,
				this);
		alaska.setColorSignature(-9666560);
		alaska.setPoint(new Point(45, 25));
		alberta.setColorSignature(-256);
		alberta.setPoint(new Point(83, 63));
		centralAmerica.setColorSignature(-128);
		centralAmerica.setPoint(new Point(83, 158));
		easternUnitedStates.setColorSignature(-8355840);
		easternUnitedStates.setPoint(new Point(132, 113));
		greenland.setColorSignature(-2302976);
		greenland.setPoint(new Point(230, 10));
		northwestTerritory.setColorSignature(-10910425);
		northwestTerritory.setPoint(new Point(109, 36));
		ontario.setColorSignature(-7039927);
		ontario.setPoint(new Point(139, 65));
		quebec.setColorSignature(-2697638);
		quebec.setPoint(new Point(189, 66));
		westernUnitedStates.setColorSignature(-11513817);
		westernUnitedStates.setPoint(new Point(72, 99));

		alaska.addNeighbor(northwestTerritory);
		alaska.addNeighbor(kamchatka);
		northwestTerritory.addNeighbor(alberta);
		northwestTerritory.addNeighbor(ontario);
		northwestTerritory.addNeighbor(greenland);

		alberta.addNeighbor(ontario);
		alberta.addNeighbor(westernUnitedStates);

		ontario.addNeighbor(westernUnitedStates);
		ontario.addNeighbor(greenland);
		ontario.addNeighbor(quebec);
		ontario.addNeighbor(easternUnitedStates);

		westernUnitedStates.addNeighbor(easternUnitedStates);
		westernUnitedStates.addNeighbor(centralAmerica);
		easternUnitedStates.addNeighbor(centralAmerica);
		easternUnitedStates.addNeighbor(quebec);
		quebec.addNeighbor(greenland);

		centralAmerica.addNeighbor(venezuela);
		venezuela.addNeighbor(peru);
		venezuela.addNeighbor(brazil);
		peru.addNeighbor(brazil);
		peru.addNeighbor(argentina);
		brazil.addNeighbor(argentina);
		brazil.addNeighbor(northAfrica);

		greenland.addNeighbor(iceland);
		iceland.addNeighbor(britain);
		iceland.addNeighbor(scandinavia);
		iceland.addNeighbor(quebec);
		iceland.addNeighbor(greenland);
		britain.addNeighbor(scandinavia);
		britain.addNeighbor(westernEurope);
		britain.addNeighbor(northernEurope);
		scandinavia.addNeighbor(northernEurope);
		scandinavia.addNeighbor(ukraine);
		northernEurope.addNeighbor(ukraine);
		northernEurope.addNeighbor(westernEurope);
		northernEurope.addNeighbor(southernEurope);
		westernEurope.addNeighbor(southernEurope);
		ukraine.addNeighbor(southernEurope);
		ukraine.addNeighbor(ural);
		ukraine.addNeighbor(afghanistan);
		ukraine.addNeighbor(middleEast);

		southernEurope.addNeighbor(middleEast);
		southernEurope.addNeighbor(egypt);
		egypt.addNeighbor(middleEast);
		egypt.addNeighbor(eastAfrica);
		egypt.addNeighbor(northAfrica);
		northAfrica.addNeighbor(westernEurope);
		northAfrica.addNeighbor(centralAfrica);

		eastAfrica.addNeighbor(middleEast);
		eastAfrica.addNeighbor(centralAfrica);
		eastAfrica.addNeighbor(madagascar);
		eastAfrica.addNeighbor(southAfrica);
		southAfrica.addNeighbor(centralAfrica);
		southAfrica.addNeighbor(eastAfrica);
		southAfrica.addNeighbor(madagascar);

		ural.addNeighbor(afghanistan);
		ural.addNeighbor(siberia);
		ural.addNeighbor(china);
		afghanistan.addNeighbor(china);
		afghanistan.addNeighbor(india);
		afghanistan.addNeighbor(middleEast);
		middleEast.addNeighbor(india);

		india.addNeighbor(siam);
		india.addNeighbor(china);
		china.addNeighbor(siberia);
		china.addNeighbor(mongolia);
		china.addNeighbor(siam);
		mongolia.addNeighbor(japan);
		mongolia.addNeighbor(irkutsk);
		mongolia.addNeighbor(siberia);
		mongolia.addNeighbor(kamchatka);
		kamchatka.addNeighbor(japan);
		kamchatka.addNeighbor(irkutsk);
		kamchatka.addNeighbor(yakutsk);
		yakutsk.addNeighbor(irkutsk);
		yakutsk.addNeighbor(siberia);
		irkutsk.addNeighbor(siberia);

		siam.addNeighbor(indonesia);
		indonesia.addNeighbor(newGuinea);
		indonesia.addNeighbor(westernAustralia);
		newGuinea.addNeighbor(easternAustralia);
		newGuinea.addNeighbor(westernAustralia);
		westernAustralia.addNeighbor(easternAustralia);

		System.out.println("Classic Map done constructing");

		this.setChanged();
		this.notifyObservers(this);
	}

	/**
	 * When ClassicMap needs to be updated, it first checks to see that the
	 * observable is a continent, then checks which continent is making the
	 * call, to avoid going through all continents. It then replaces its
	 * continent with the newer continent. Then it notifies observers.
	 * 
	 * @param o
	 *            is a observable class which has notify changes.F
	 * 
	 * @param arg
	 *            is the data changed in the o.
	 */
	public void update(Observable o, Object arg) {
		if (o instanceof Continent) {
			if (((Continent) arg).getContinentName() == this.northAmerica
					.getContinentName()) {
				this.northAmerica = (Continent) arg;
				this.setChanged();
				this.notifyObservers(this);
			}
			if (((Continent) arg).getContinentName() == this.southAmerica
					.getContinentName()) {
				this.southAmerica = (Continent) arg;
				this.setChanged();
				this.notifyObservers(this);
			}
			if (((Continent) arg).getContinentName() == this.africa
					.getContinentName()) {
				this.africa = (Continent) arg;
				this.setChanged();
				this.notifyObservers(this);
			}
			if (((Continent) arg).getContinentName() == this.europe
					.getContinentName()) {
				this.europe = (Continent) arg;
				this.setChanged();
				this.notifyObservers(this);
			}
			if (((Continent) arg).getContinentName() == this.asia
					.getContinentName()) {
				this.asia = (Continent) arg;
				this.setChanged();
				this.notifyObservers(this);
			}
			if (((Continent) arg).getContinentName() == this.oceania
					.getContinentName()) {
				this.oceania = (Continent) arg;
				this.setChanged();
				this.notifyObservers(this);
			}
		}
	}
}
